<?php
namespace Badmins\Controller;
use Think\Controller;
class CkadminController extends Controller{
	//session('untv_admin_id')?session('untv_admin_id'):'1';
	public function __construct(){
		parent::__construct();
		$admin_id = session('bxd_shopping_admin_id');
		if(empty($admin_id)){
			$url=__APP__."/".MODULE_NAME."/Public/login";
			echo "<script language='javascript'>";
			echo "parent.parent.location='$url'";
			echo "</script>";die();
		}
	}
	/**
	 * 获取后台用户的信息
	 */
	public function admin_infor(){
		$id = session('bxd_shopping_admin_id');
		$list = M('admin as a')->join("left join ".C('DB_PREFIX')."role as r on a.role_id = r.id")->where("a.id='{$id}'")->field("a.*,r.name as rolename,r.rights")->find();
		return $list;
	}
	/**
	 * 生成主键--uuid
	 */
	public function parekey($model){
		$db = M($model);
		for($k=0;$k<=150000000;$k++){
			$uuid = \Org\Util\String::randString(8,0,'123456789');
			$re = $db->where(array('id'=>$uuid))->find();
			if(empty($re)){
				break;
			}else{
			}
		}
		return $uuid;
	}
	/**
	 * 添加 操作日志
	 */
	public function add_log($log){
		$db = M('operation_log');
		$data['admin_id'] = session('bxd_shopping_admin_id')?session('bxd_shopping_admin_id'):'1';
		$data['operation_ip'] = get_client_ip();
		$data['log'] = $log;
		$db->add($data);
	}
	/**
	 * 操作下级区域
	 * @param unknown $id
	 * @param unknown $level
	 * @param unknown $status
	 */
	public function update_next_area($id,$level,$status){
		$db = M('Region');
		switch ($level){
			case '3':
				$db->where("parent_id='{$id}'")->setField('status',$status);
				break;
			case '2':
				$ids = $db->where("parent_id='{$id}'")->field("id")->select();
				foreach ($ids as $va) {
					$idarray[] = $va['id'];
				}
				$db->where("parent_id='{$id}'")->setField('status',$status);
				$data['parent_id'] = array('in',$idarray);
				$dayta['level'] = 4;
				$db->where($data)->setField('status',$status);
				break;
			case '1':
				$ids = $db->where("parent_id='{$id}'")->field("id")->select();
				foreach ($ids as $va) {
					$idarray[] = $va['id'];
				}
				$data['parent_id'] = array('in',$idarray);
				$data['level'] = 3;
				$nextid = $db->where($data)->field("id")->select();
				foreach ($nextid as $k=>$vk){
					$nextids[] = $vk['id'];
				}
				$map['parent_id'] = array('in',$nextids);
				$map['level'] = 4;
				$db->where("parent_id='{$id}'")->setField('status',$status);
				$db->where($map)->setField('status',$status);
				$db->where($data)->setField('status',$status);
			break;
		}
	}
	/**
	 * 操作下级分类
	 */
	public function update_next_class($id,$level,$field,$status){
		$db = M('category');
		switch ($level){
			case '2':
				$db->where("parent_id='{$id}'")->setField($field,$status);
			break;
			case '1':
				$db->where("parent_id='{$id}'")->setField('status',$status);
				$ids = $db->where("parent_id='{$id}'")->field("id")->select();
				foreach ($ids as $va) {
					$idarray[] = $va['id'];
				}
				$data['parent_id'] = array('in',$idarray);
				$dayta['level'] = 3;
				$db->where($data)->setField($field,$status);
			break;
		}
	}
}